<?php

class Aplication
{			
	
			
	function Aplication()
	{
		
	}
	
	//////////////////////////////////////////////////////////////////////////////////
	// addAction: Esta funcion es la encargada de agregar cada accion q ocurra por contacto.
	///////////////////////////////////////////////////////////////////////////////		
	public function addAction($data)
	{
		$sql = "
		INSERT INTO `log_application` (
		`loap_code`,
		`mod_code`,
		`use_code`, 
		`cont_code`,
		`code_aditional`, 
		`loap_date`, 
		`loap_action`) VALUES 
		(NULL, '".$data->mod_code."', '".$data->user_code."', '".$data->cont_code."', '".$data->aditional_code."','".date('Y-m-d H:i:s')."', '".$data->action."');";
	
		return mysql_query($sql);
		
	}
				
	function getHistoryByContact($code)
	{
		$sql = "SELECT * FROM log_application WHERE cont_code = ".$code." GROUP BY loap_date ORDER BY loap_date ";
		
		$consult  = mysql_query($sql);
		$i = 0;
		while($row = mysql_fetch_array($consult))
		{
			$data[$i]->loap_code   		= $row['loap_code']; 
			$data[$i]->code     		= $row['cont_code'];
			$data[$i]->mod_code     	= $row['mod_code'];
			$data[$i]->use_code    		= $row['use_code'];					
			$data[$i]->cont_code  		= $row['cont_code'];
			$data[$i]->code_aditional   = $row['code_aditional'];
			$data[$i]->loap_action		= $row['loap_action'];
			$data[$i]->loap_date		= $row['loap_date'];
			
			if($data[$i]->mod_code==1)//assing to seller								
			{
				$sql2      = "SELECT use_name FROM user WHERE use_code=".$data[$i]->use_code;
				$consult2  = mysql_query($sql2);		
				$row2 	   = mysql_fetch_array($consult2);
				
				$data[$i]->use_name         = $row2['use_name'];
			}
			
			if($data[$i]->mod_code==2)//task
			{
				$sql2      = "SELECT task_time,task_comment FROM task_scheduling  WHERE task_code=".$data[$i]->code_aditional;
				$consult2  = mysql_query($sql2);		
				$row2 	   = mysql_fetch_array($consult2);
				
				$data[$i]->task_time 			= $row2['task_time'];
				$data[$i]->task_comment 		= $row2['task_comment'];
			}
			
			if($data[$i]->mod_code==3)//note
			{
				$sql2      = "SELECT note_content FROM note  WHERE note_code=".$data[$i]->code_aditional;
				$consult2  = mysql_query($sql2);		
				$row2 	   = mysql_fetch_array($consult2);
				
				$data[$i]->note_content = $row2['note_content'];
			}
			
			$i++;				
		}
		return $data;	
	}
	
	///////////////////////////////////////////////////////////////////////////////
	// countHistory: Esta funcion permite saber en TOTAL cuantos registros existen en el historial.	
	///////////////////////////////////////////////////////////////////////////////
	public function countHistory($search='',$code)
	{
		$where = '';
		if($search != '')
			$where = " AND (loap_date like '%".$search."%')";		
		
		$sql = "SELECT count(*) AS cant FROM log_application WHERE cont_code = ".$code.$where;
	
		$consulta = mysql_query($sql);
		$row = mysql_fetch_array($consulta);
			
		return $row['cant'];
	}
	
	
}
?>